package com.yiren.dbaa.module.practice_set.site.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date : 2022/9/20 16:18
 * @description: 考场规则适配器
 */
public class SiteRuleAdapter extends BaseAdapter {

    private Context context;
    private List<SiteEntity> list;
    public int mSelect = 0;
    private int type = 0;   //0:考场规则首页适配器  1:新建规则共享列表适配器
    private boolean isPartner;//是否是合伙人

    public SiteRuleAdapter(Context context, List<SiteEntity> list, int type) {
        this.context = context;
        this.list = list;
        this.type = type;
        isPartner = LoadConfigApi.loadUserParamConfig(context).isPartner();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.item_site_rule, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SiteEntity site = list.get(position);
        holder.tvSiteName.setText(site.getSiteName());
        if (type == 0) {
            //合伙人视角
            if (isPartner) {
                holder.tvUpdateBtn.setVisibility(View.GONE);
                //可更新
                holder.tvUpdate.setVisibility(site.isUpdate() ? View.VISIBLE: View.GONE);
                //是否下载
                holder.tvManage.setText(site.isDownLoad() ? "管理" : "下载");
                //未发布的考规
                if (site.getRuleVer() == 0) {
                    holder.tvRuleVersion.setText("未公开发布，暂无版本");
                    holder.tvDelete.setVisibility(View.VISIBLE);
                    holder.tvUsingRange.setVisibility(View.GONE);
                    holder.tvManage.setText("管理");
                    holder.vBar.setVisibility(View.GONE);
                } else {
                    holder.vBar.setVisibility(View.VISIBLE);
                    holder.tvUsingRange.setVisibility(View.VISIBLE);
                    holder.tvRuleVersion.setText("规则版本：V" + list.get(position).getRuleVer());
                    holder.tvUsingRange.setText("使用范围：" + (site.getUseRange() == 1 ? "个别用户使用" : "全部用户使用"));
                    holder.tvDelete.setVisibility(View.GONE);
                }
            } else {  //教练视角
                holder.tvUpdate.setVisibility(View.GONE);
                holder.tvUsingRange.setVisibility(View.GONE);
                holder.vBar.setVisibility(View.GONE);
                //是否下载
                holder.tvManage.setText(site.isDownLoad() ? "编辑规则" : "下载");
                //未发布的考规
                if (site.getRuleVer() == 0) {
                    holder.tvRuleVersion.setText("未公开发布，暂无版本");
                    holder.tvDelete.setVisibility(View.VISIBLE);
                    holder.tvUsingRange.setVisibility(View.GONE);
                    holder.tvUpdateBtn.setVisibility(View.GONE);
                    holder.tvManage.setText("编辑规则");
                } else {
                    holder.tvRuleVersion.setText("规则版本：V" + site.getRuleVer());
                    holder.tvDelete.setVisibility(View.GONE);
                    holder.tvUpdateBtn.setVisibility(site.isUpdate() ? View.VISIBLE : View.GONE);
                }
            }
        } else if (type == 1) {
            holder.tvUpdate.setVisibility(View.GONE);
            holder.tvDelete.setVisibility(View.GONE);
            holder.tvManage.setText("选中新建");
            holder.tvRuleVersion.setText("考车系统：" + site.getC_name());
            holder.tvUsingRange.setText("共享人：" + site.getUserId());
        }
        holder.tvUpdateBtn.setOnClickListener(v -> {
            if (mOnUpdateClickListener != null) {
                mOnUpdateClickListener.onUpdateClick(position);
            }
        });
        holder.tvDelete.setOnClickListener(v -> {
            if (mOnDeleteClickListener != null) {
                mOnDeleteClickListener.onDeleteClick(position);
            }
        });
        holder.tvManage.setOnClickListener(v -> {
            if (mOnManageClickListener != null) {
                mOnManageClickListener.onManageClick(position);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_site_name)
        TextView tvSiteName;
        @BindView(R.id.tv_update)
        TextView tvUpdate;
        @BindView(R.id.tv_rule_version)
        TextView tvRuleVersion;
        @BindView(R.id.v_bar)
        View vBar;
        @BindView(R.id.tv_using_range)
        TextView tvUsingRange;
        @BindView(R.id.tv_update_btn)
        TextView tvUpdateBtn;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_manage)
        TextView tvManage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //更新点击回调
    public interface OnUpdateClickListener {
        void onUpdateClick(int position);
    }

    private OnUpdateClickListener mOnUpdateClickListener;

    public void setOnUpdateClickListener(OnUpdateClickListener onUpdateClickListener) {
        mOnUpdateClickListener = onUpdateClickListener;
    }

    //条目删除回调
    public interface OnDeleteClickListener {
        void onDeleteClick(int position);
    }

    private OnDeleteClickListener mOnDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    //管理点击回调
    public interface OnManageClickListener {
        void onManageClick(int position);
    }

    private OnManageClickListener mOnManageClickListener;

    public void setOnManageClickListener(OnManageClickListener onManageClickListener) {
        mOnManageClickListener = onManageClickListener;
    }

    public void changeSelected(int positon) {
        //刷新方法
        if (positon != mSelect) {
            mSelect = positon;
            notifyDataSetChanged();
        }
    }

}